<?php

namespace Admin\Controller;

use Think\Controller;

class TestController extends AdminController
{

    public function index()
    {
        $arr = array(
            array(1),
            array(2, 3),
            array('a', 'b', 'c'),
        );
        $aa = $this->dikaer($arr);
        $bb = $this->xxx();
        var_dump($bb);
    }

    public function dikaer($arr)
    {
        $arr1   = array();
        $result = array_shift($arr);
        while ($arr2 = array_shift($arr)) {
            $arr1   = $result;
            $result = array();
            foreach ($arr1 as $v) {
                foreach ($arr2 as $v2) {
                    if (!is_array($v)) {
                        $v = array($v);
                    }

                    if (!is_array($v2)) {
                        $v2 = array($v2);
                    }

                    $result[] = array_merge_recursive($v, $v2);
                }
            }
        }
        return $result;
    }

     public function xxx()
    {
        // 查询所有顶级分类
        $dingji = M('Category')->where('pid=0')->getField('id', true);

        // 随机取三个顶级分类
        $arr    = array_rand(array_flip($dingji), 3);
        $tmp    = [];

        // 把取到的三个顶级分类下面的子分类全部猎取出来
        foreach ($arr as $v) {
            $tmp[] = $this->digui($v, 'id,title,pid');
        }

        // 把猎取到的子分类的id 分类放到数组中
        $whereArr = [];
        foreach ($tmp as $k => $v) {
            foreach ($v as $vv) {
                $whereArr[$k][] = $vv['id'];
            }
        }
        
        // 根据子分类的id 查询 商品 cate id数组当中的商品
        foreach ($whereArr as $k => $v) {
            var_dump($v);
            $where['pid'] = ['in', $v];
            $goods[]       = M('goods')->where($where)->select();
        }
       
    }

    public function digui($pid = 0, $field = true, $arr = [], $lv = 0)
    {
        $cate = M('Category')->where('pid=' . $pid)->field($field)->select();
        if ($cate) {
            $lv++;
            foreach ($cate as &$v) {
                $v['lv'] = $lv;
                $arr[]   = $v;
                $arr     = $this->digui($v['id'], $field, $arr, $lv);
            }
        }
        return $arr;
    }
}
